<template>
    <div id="app">
      <el-form :model="student" label-width="100px" :rules="formRules" ref="abc">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="student.name"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="pwd">
          <el-input v-model="student.pwd"></el-input>
        </el-form-item>

        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="student.gender">
            <el-radio label="1">男</el-radio>
            <el-radio label="0">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="电话" prop="description">
          <el-input v-model="student.tel"></el-input>
        </el-form-item>
        <el-form-item label="学号" prop="stuNo">
          <el-input v-model="student.stuNo"></el-input>
        </el-form-item>

        <el-form-item label="班级">
          <el-select v-model="student.cid" placeholder="请选择">
            <el-option
              v-for="item in classList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click.prevent="test">保存</el-button>
        </el-form-item>
      </el-form>
    </div>
</template>

<script>
    export default {
      data(){
        return {
          classList:[],
          student:{
            name:""
          },
          //定义表单的验证规则
          formRules:{//表单校验规则
            name:[
              //required：true表示该框是必填的。message表示用户出错后提示的信息，trigger是指什么时候触发验证
              {required:true,message:"名称不能为空",trigger:"blur"}
            ]
          }
        }
      },
      created() {
        this.axios.get("/class/selectAll").then((resp)=>{
          this.classList=resp.data;
        });
      },
      methods:{
        test(){
          this.$refs["abc"].validate((valid)=>{
            //valid表示表单是否验证通过。
            if(valid){
              //发出保存请求
              this.$confirm("是否确认提交","保存提示").then(()=>{
                this.axios.post("/student/add",this.student).then((resp)=>{
                    this.$message.success("保存成功！");
                    this.$router.push("/classstudent/StudentList")
                  });
              }).catch(()=>{});

            }
          })
        }
      }
    }
</script>


<style scoped>

</style>
